<?php 
require '../../frame/base.php';

$tag = !empty($_REQUEST['tag']) ? $_REQUEST['tag'] : null;
$set = !empty($_REQUEST['set']) ? json_decode($_REQUEST['set']) : null;
$IDS = !empty($_REQUEST['IDS']) ? $_REQUEST['IDS'] : null;
$uid = !empty($_REQUEST['uid']) ? $_REQUEST['uid'] : null;
$sid = !empty($_REQUEST['sid']) ? $_REQUEST['sid'] : null;
$wxName = !empty($_REQUEST['wxName']) ? $_REQUEST['wxName'] : null;
$zfbName = !empty($_REQUEST['zfbName']) ? $_REQUEST['zfbName'] : null;
$bankName = !empty($_REQUEST['bankName']) ? $_REQUEST['bankName'] : null;
$bindphone = !empty($_REQUEST['bindphone']) ? $_REQUEST['bindphone'] : null;

define('KEYS','$10@w/2e3U4H');

if($tag=='bind'){
if($uid){
	if($wxName){
		$Db->table('blind_wei')->insert([
			'uid'=>$uid,
			'weipay'=>$_REQUEST['weipay'],
			'weixin_name'=>$_REQUEST['wxName'],
			'realname'=>$_REQUEST['realname'],
			'telphone'=>$_REQUEST['telphone'],
		]);
	}

	if($zfbName){
		$Db->table('blind_alipay')->insert([
			'uid'=>$uid,
			'alipay'=>$_REQUEST['alipay'],
			'alipay_name'=>$_REQUEST['zfbName'],
			'realname'=>$_REQUEST['realname'],
			'telphone'=>$_REQUEST['telphone'],
		]);
	}

	if($bankName){
		$Db->table('blind_banks')->insert([
			'uid'=>$uid,
			'bank_account'=>$_REQUEST['bank_account'],
			'bid'=>$_REQUEST['bankName'],
			'realname'=>$_REQUEST['realname'],
			'telphone'=>$_REQUEST['telphone'],
		]);
	}

	if($bindphone){
		$Db->table('user')->where('userid',$uid)->update([
			'bindphone'=>$bindphone,
		]);
	}

	if($set){
		$passwd=KEYS.$set->passwd;
		$Db->table('user')->where('userid',$uid)->update(['passwd'=>$passwd]);
	}
	
}elseif($sid){
	if($wxName){
		$Db->table('blind_wei')->insert([
			'sid'=>$sid,
			'weipay'=>$_REQUEST['weipay'],
			'weixin_name'=>$_REQUEST['wxName'],
			'weixin_name'=>$_REQUEST['weixin_name'],
			'telphone'=>$_REQUEST['telphone'],
		]);
	}

	if($zfbName){
		$Db->table('blind_alipay')->insert([
			'sid'=>$sid,
			'alipay'=>$_REQUEST['alipay'],
			'alipay_name'=>$_REQUEST['zfbName'],
			'realname'=>$_REQUEST['realname'],
			'telphone'=>$_REQUEST['telphone'],
		]);
	}

	if($bankName){
		$Db->table('blind_banks')->insert([
			'sid'=>$sid,
			'bank_account'=>$_REQUEST['bank_account'],
			'bid'=>$_REQUEST['bankName'],
			'realname'=>$_REQUEST['realname'],
			'telphone'=>$_REQUEST['telphone'],
		]);
	}

	if($bindphone){
		$Db->table('shop')->where('shopid',$sid)->update([
			'bindphone'=>$bindphone,
		]);
	}

	if($set){
		$passwd=KEYS.$set->passwd;
		$Db->table('shop')->where('shopid',$sid)->update(['passwd'=>$passwd]);
	}	
}
	echo json_encode(['code'=>200,'msg'=>'成功']);
}elseif($tag=='modify'){

if($uid){
	if($wxName){
		$Db->table('blind_wei')->where(['uid'=>$uid,'id'=>$IDS])->update([
			'weipay'=>$_REQUEST['weipay'],
			'weixin_name'=>$wxName,
			'realname'=>$_REQUEST['realname'],
			'telphone'=>$_REQUEST['telphone'],
		]);
	}

	if($zfbName){
		$Db->table('blind_alipay')->where(['uid'=>$uid,'id'=>$IDS])->update([
			'alipay'=>$_REQUEST['alipay'],
			'alipay_name'=>$zfbName,
			'realname'=>$_REQUEST['realname'],
			'telphone'=>$_REQUEST['telphone'],
		]);
	}

	if($bankName){
		$Db->table('blind_banks')->where(['uid'=>$uid,'id'=>$IDS])->update([
			'bank_account'=>$_REQUEST['bank_account'],
			'bid'=>$bankName,
			'realname'=>$_REQUEST['realname'],
			'telphone'=>$_REQUEST['telphone'],
		]);
	}

	if($set){
		$passwd=KEYS.$set->passwd;
		$Db->table('user')->where('userid',$uid)->update(['passwd'=>$passwd]);
	}
	
}elseif($sid){
	if($wxName){
		$Db->table('blind_wei')->where(['sid'=>$sid,'id'=>$IDS])->update([
			'weipay'=>$_REQUEST['weipay'],
			'weixin_name'=>$wxName,
			'realname'=>$_REQUEST['realname'],
			'telphone'=>$_REQUEST['telphone'],
		]);
	}

	if($zfbName){
		$Db->table('blind_alipay')->where(['sid'=>$sid,'id'=>$IDS])->update([
			'alipay'=>$_REQUEST['alipay'],
			'alipay_name'=>$zfbName,
			'realname'=>$_REQUEST['realname'],
			'telphone'=>$_REQUEST['telphone'],
		]);
	}

	if($bankName){
		$Db->table('blind_banks')->where(['sid'=>$sid,'id'=>$IDS])->update([
			'bank_account'=>$_REQUEST['bank_account'],
			'bid'=>$bankName,
			'realname'=>$_REQUEST['realname'],
			'telphone'=>$_REQUEST['telphone'],
		]);
	}

	if($set){
		$passwd=KEYS.$set->passwd;
		$Db->table('shop')->where('shopid',$sid)->update(['passwd'=>$passwd]);
	}
}
	echo json_encode(['code'=>200,'msg'=>'成功']);

}


// 加密
function encrypt($str, $key = '$1@9')
{
    $coded = '';
    $keylength = strlen($key);

    for ($i = 0, $count = strlen($str); $i < $count; $i += $keylength)
    {
        $coded .= substr($str, $i, $keylength) ^ $key;
    }

    return str_replace('=', '', base64_encode($coded));
}


// 解密
function decrypt($str, $key = '$1@9')
{
    $coded = '';
    $keylength = strlen($key);
    $str = base64_decode($str);

    for ($i = 0, $count = strlen($str); $i < $count; $i += $keylength)
    {
        $coded .= substr($str, $i, $keylength) ^ $key;
    }

    return $coded;
}